package com.cuit.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.eduservice.entity.EduSubject;
import com.cuit.eduservice.entity.excel.SubjectData;
import com.cuit.eduservice.entity.subject.OneSubject;
import com.cuit.eduservice.entity.subject.TwoSubject;
import com.cuit.eduservice.listener.SubjectExcelListener;
import com.cuit.eduservice.mapper.EduSubjectMapper;
import com.cuit.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author horizon
 * @since 2020-09-16
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream in=file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> findAllSubject() {
        //1.查询一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2.查询二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        //parent_id不为0
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建集合，封装最终数据
        List<OneSubject> OneFinalSubjectList = new ArrayList<>();

        //3.封装一级分类
        for (EduSubject eduOneSubject : oneSubjectList) {
            OneSubject oneSubject=new OneSubject();
            //oneSubject.setId(eduSubject.getId());
            //oneSubject.setTitle(eduSubject.getTitle());
            //把eduSubject中get的值放在oneSubject的set中
            BeanUtils.copyProperties(eduOneSubject,oneSubject);
            OneFinalSubjectList.add(oneSubject);
            //4.封装二级分类
            //在一级分类循环中遍历二级分类
            List<TwoSubject> TwoFinalSubjectList = new ArrayList<>();
            for (EduSubject eduTwoSubject : twoSubjectList) {
                //判断二级分类的parent_id与一级分类是否相同
                if(eduTwoSubject.getParentId().equals(eduOneSubject.getId())){
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(eduTwoSubject,twoSubject);
                    TwoFinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(TwoFinalSubjectList);
        }

        return OneFinalSubjectList;
    }
}
